package com.wang.users.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.wang.common.dao.impl.BaseDaoImpl;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.dao.IAMenuDao;
import com.wang.users.pojo.AMenu;

public class AMenuDaoImpl extends BaseDaoImpl<AMenu> implements IAMenuDao
{
	@Override
	public int saveOne(AMenu menu)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("insert into a_menu (parentId, name, url, content, showStatus, leafStatus, status, createTime, updateTime, pubTime) ");
		sb.append(" values ");
		sb.append(" (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
		
		paramsList.add(menu.getParentId());
		paramsList.add(menu.getName());
		paramsList.add(menu.getUrl());
		paramsList.add(menu.getContent());
		paramsList.add(menu.getShowStatus());
		paramsList.add(menu.getLeafStatus());
		paramsList.add(menu.getStatus());
		paramsList.add(menu.getCreateTime());
		paramsList.add(menu.getUpdateTime());
		paramsList.add(menu.getPubTime());
		
		/**
		 * 执行sql语句
		 */
		int res =  executeSql(sb.toString(),paramsList);
		/* 处理主键 */
		int id = (int) paramsList.get(paramsList.size() - 1);
		menu.setId(id);
		return res ; 
	}

	@Override
	public int updateOne(AMenu menu)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("update a_menu set ");
		sb.append(" parentId= ?, name= ?, url= ?, content= ?, showStatus= ?, leafStatus= ?, status= ?, createTime= ?, updateTime= ?, pubTime = ? ");
		sb.append(" where id = ? ");
		
		paramsList.add(menu.getParentId());
		paramsList.add(menu.getName());
		paramsList.add(menu.getUrl());
		paramsList.add(menu.getContent());
		paramsList.add(menu.getShowStatus());
		paramsList.add(menu.getLeafStatus());
		paramsList.add(menu.getStatus());
		paramsList.add(menu.getCreateTime());
		paramsList.add(menu.getUpdateTime());
		paramsList.add(menu.getPubTime());
		paramsList.add(menu.getId());
		
		/* 执行sql语句 */
		return this.executeSql(sb.toString(), paramsList);
	}

	@Override
	public int deleteOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("delete from a_menu ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else
		{
			/* 不传条件不能删除 */
			return 0 ; 
		}
		
		/* 执行sql语句 */
		return this.executeSql(sb.toString(), paramsList);
	}

	@Override
	public AMenu findOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_menu ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else
		{
			return null ; 
		}
		
		return findOneSql(sb.toString(), paramsList,AMenu.class);
	}

	@Override
	public List<AMenu> findCondList(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_menu where 1 = 1 ");
		
		/* 关键字 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			sb.append(" and (name like ? or content like ? ) ");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
		}
		
		/* 状态 */
		if(condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sb.append(" and status = ?  ");
			paramsList.add(condMap.get("status"));
		}
		
		/* 时间 */
		if(condMap.get("st") instanceof Date && condMap.get("ed") instanceof Date )
		{
			sb.append(" and pubTime >= ? and pubTime < ? ");
			paramsList.add(condMap.get("st"));
			paramsList.add(condMap.get("ed"));
		}
		
		/* parentId
		 * 	parentId is null;就想成为null;可以查询所有的一级节点(顶级节点)
		 * 		select * from a_menu where parentId is null ; (parentId==0的时候是一级节点)为啥变，因为pojo的parentId的类型是int(0);不是integer(null)
		 * 		select * from a_menu where parentId = 0 
		 * 	查询某个节点的子节点;
		 * 		select * from a_menu where parentId = 5;
		 * 
		 *  如果不想查询parentId;直接不传parentId就可以;(这个简单)
		 *  
		 *  如果一级节点,parentId===null而不是0;
		 *  	pojo的parentId类型改为integer;
		 *  select * from a_menu where parentId is null(查询一级节点);condMap中的parentId必须传一个字符串或者固定的值(-100)
		 *  select * from a_menu where parentId = 5 ;(查询5下面的子节点);condMap中的parentId的值为节点的id;
		 *  select * from a_menu ;
		 *  
		 *  以前的做法，重新引入一个新的值,condMap中的parnetId要想使用得再放一个值:parentIdFlag=true;
		 *  */
		if(condMap.get("parentId") != null && !"".equalsIgnoreCase(condMap.get("parentId") + ""))
		{
			sb.append(" and parentId = ?  ");
			paramsList.add(condMap.get("parentId"));
		}
		
		/* 排序 */
		if("idDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sb.append(" order by id desc ") ; 
		}else
		{
			sb.append(" order by pubTime desc ");
		}
		
		if(pageInfoUtil != null)
		{
			return this.findListSql(pageInfoUtil, sb.toString(), paramsList,AMenu.class);
		}
		return this.findListSql(sb.toString(), paramsList,AMenu.class); 
	}

}
